package com.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bean.*;
import com.service.DayRealtimeService;
import com.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/average")
public class DayController {

    @Autowired
    private DayRealtimeService dayRealtimeService;

    public String dayRealtime() {
        List<DayRealtime> dayRealtimes = dayRealtimeService.DayRealtime();
        JSONObject resultJson = new JSONObject();
        resultJson.put("status", 0);
        resultJson.put("msg", "");

        JSONObject dataJson = new JSONObject();
        resultJson.put("data", dataJson);

        JSONArray seriesArray = new JSONArray();
        dataJson.put("series", seriesArray);

        JSONObject seriesObj = new JSONObject();
        seriesObj.put("name", "series1");
        seriesArray.add(seriesObj);

        JSONArray dataArray = new JSONArray();
        seriesObj.put("data", dataArray);

        for (DayRealtime dayRealtime : dayRealtimes) {

            JSONObject dataItem = new JSONObject();
            dataItem.put("name", dayRealtime.getStockName());
            dataItem.put("value", dayRealtime.getVolumeRatio());
            dataArray.add(dataItem);
        }
        return resultJson.toJavaObject(String.class);
    }

    @Autowired
    private MinutesMarketService minutesMarketService;

    @RequestMapping("/minutesMarket")
    public String minutesMarket() {
        List<MinutesMarket> minutesMarkets = minutesMarketService.getMinutesMarket();
        JSONObject resultJson = new JSONObject();
        resultJson.put("status", 0);
        resultJson.put("msg", "");

        JSONObject dataJson = new JSONObject();
        resultJson.put("data", dataJson);

        JSONArray seriesArray = new JSONArray();
        dataJson.put("series", seriesArray);

        JSONObject seriesObj = new JSONObject();
        seriesObj.put("name", "series1");
        seriesArray.add(seriesObj);

        JSONArray dataArray = new JSONArray();
        seriesObj.put("data", dataArray);

        for (MinutesMarket minutesMarket : minutesMarkets) {
            JSONObject dataItem = new JSONObject();
            dataItem.put("name", minutesMarket.getStockName());
            dataItem.put("value", minutesMarket.getPriceChangeRate());
            dataArray.add(dataItem);
        }
        return resultJson.toJavaObject(String.class);
    }

    @Autowired
    private SecondStockSerice secondStockSerice;

    @RequestMapping("/secondStock")
    public String secondStock() {
        List<SecondStock> secondStocks = secondStockSerice.getSecondStock();
        JSONObject resultJson = new JSONObject();
        resultJson.put("status", 0);
        resultJson.put("msg", "");

        JSONObject dataJson = new JSONObject();
        resultJson.put("data", dataJson);

        JSONArray seriesArray = new JSONArray();
        dataJson.put("series", seriesArray);

        JSONObject seriesObj = new JSONObject();
        seriesObj.put("name", "series1");
        seriesArray.add(seriesObj);

        JSONArray dataArray = new JSONArray();
        seriesObj.put("data", dataArray);

        for (SecondStock secondStock : secondStocks) {
            JSONObject dataItem = new JSONObject();
            dataItem.put("name", secondStock.getStockName());
            dataItem.put("value", secondStock.getPrice());
            dataArray.add(dataItem);
        }
        return resultJson.toJavaObject(String.class);
    }

    @Autowired
    private RealtimeRiseService realtimeRiseService;

    @RequestMapping("/realtimeRise")
    public String realtimeRise() {
        List<RealtimeRise> realtimeRises = realtimeRiseService.getRealtimeRise();
        JSONObject resultJson = new JSONObject();
        resultJson.put("status", 0);
        resultJson.put("msg", "");

        JSONObject dataJson = new JSONObject();
        resultJson.put("data", dataJson);

        JSONArray seriesArray = new JSONArray();
        dataJson.put("series", seriesArray);

        JSONObject seriesObj = new JSONObject();
        seriesObj.put("name", "series1");
        seriesArray.add(seriesObj);

        JSONArray dataArray = new JSONArray();
        seriesObj.put("data", dataArray);

        for (RealtimeRise realtimeRis : realtimeRises) {
            JSONObject dataItem = new JSONObject();
            dataItem.put("name", realtimeRis.getStockCode());
            dataItem.put("value", realtimeRis.getCurrentPrice());
            dataArray.add(dataItem);
        }

        return resultJson.toJavaObject(String.class);
    }

    @Autowired
    private FiveMinutesService fiveMinutesService;

    @RequestMapping("/fiveMinutes")
    public String fiveMinutes() {
        List<FiveMinutes> fiveMinutes = fiveMinutesService.getFiveMinutes();
        JSONObject resultJson = new JSONObject();
        resultJson.put("status", 0);
        resultJson.put("msg", "");

        JSONObject dataJson = new JSONObject();
        resultJson.put("data", dataJson);

        JSONArray seriesArray = new JSONArray();
        dataJson.put("series", seriesArray);

        JSONObject seriesObj = new JSONObject();
        seriesObj.put("name", "series1");
        seriesArray.add(seriesObj);

        JSONArray dataArray = new JSONArray();
        seriesObj.put("data", dataArray);

        for (FiveMinutes fiveMinute : fiveMinutes) {
            JSONObject dataItem = new JSONObject();
            dataItem.put("name", fiveMinute.getStockName());
            dataItem.put("value", fiveMinute.getCurrentPrice());
            dataArray.add(dataItem);
        }

        return resultJson.toJavaObject(String.class);
    }
}
